#include <iostream>
#include "asynctask.hpp"
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <random>  // 包含随机数库
#include <cmath>
using namespace std;

int main() {
    ofstream file("1.txt", ios::out);
    if (!file.is_open()) {
        cout << "open file failed" << endl;
        return 1;
    }
    random_device rd;  // 获取一个随机设备来作为种子
    mt19937 gen(rd()); // 使用随机设备生成一个 Mersenne Twister 伪随机数生成器
    uniform_int_distribution<int> dist(0, 255); // 定义一个均匀分布在0到255之间的整数分布
    Buffer buffer;
    buffer.reserve(10);
    auto onBufferFull = [&file](const Buffer& buffer) {
        // file.write((char*)buffer.data(), buffer.size());
        for (auto i : buffer) {
            file << char(i)  + 50 << " ";
        }
        file << endl;
    };
    const uint32_t maxBuffSize = 1024 * 512;
    AsyncTask task(onBufferFull, maxBuffSize, 3);
    while (1) {
        this_thread::sleep_for(200ms);
        for (int i = 0; i < 10; i++) {
            buffer.push_back(abs(dist(gen)) % 256);
        }
        task.append(buffer);
        buffer.clear();
    }
    return 0;
}
